package com.fancy.learncenter.ui.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.BaseBean;
import com.fancy.learncenter.bean.QRDataBean;
import com.fancy.learncenter.common.LoginUserSharePrefernce;
import com.fancy.learncenter.net.CustomNetCallBack;
import com.fancy.learncenter.net.CustomNetSubscriber;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.ui.activity.base.BaseActivity;
import com.fancy.learncenter.utils.NetUtil;
import com.fancy.learncenter.utils.SPUtils;
import com.fancy.learncenter.utils.Utils;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import java.util.Hashtable;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.ResponseBody;
import retrofit2.Response;

public class MyQRActivity extends BaseActivity {


    @Bind(R.id.simpleDraweeView)
    ImageView simpleDraweeView;
    @Bind(R.id.headerIcon)
    SimpleDraweeView headerIcon;
    @Bind(R.id.name)
    TextView name;
    @Bind(R.id.description)
    TextView description;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_qr, Status.STATUS, "借阅二维码");
        ButterKnife.bind(this);
        headerIcon.setImageURI(LoginUserSharePrefernce.getHeaderPath());
        name.setText(LoginUserSharePrefernce.getName());
        getQR();
    }


    Bitmap bitmap;

    // 要转换的地址或字符串,可以是中文
    public void createQRImage(String fid, ImageView imageView) {
        int QR_WIDTH = Utils.dip2pix(this, 300);
        int QR_HEIGHT = Utils.dip2pix(this, 300);
        try {
            // 判断URL合法性
            if (fid == null || "".equals(fid) || fid.length() < 1) {
                return;
            }
            Hashtable hints = new Hashtable();
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            // 生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
            // 图像数据转换，使用了矩阵转换
            BitMatrix bitMatrix = new QRCodeWriter().encode(fid,
                    BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints);
            int[] pixels = new int[QR_WIDTH * QR_HEIGHT];
            // 下面这里按照二维码的算法，逐个生成二维码的图片，
            // 两个for循环是图片横列扫描的结果
            for (int y = 0; y < QR_HEIGHT; y++) {
                for (int x = 0; x < QR_WIDTH; x++) {
                    if (bitMatrix.get(x, y)) {
                        pixels[y * QR_WIDTH + x] = 0xff000000;
                    } else {
                        pixels[y * QR_WIDTH + x] = 0xffffffff;
                    }
                }
            }
            // 生成二维码图片的格式，使用ARGB_8888
            bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT,
                    Bitmap.Config.ARGB_8888);

            bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT);
            // 显示到一个ImageView上面

            imageView.setImageBitmap(bitmap);

        } catch (WriterException e) {
            e.printStackTrace();
        }
    }

    private void getQR() {
        HttpMehtod.getInstance().getQRCode(new CustomNetSubscriber(this) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {
                //解析数据
                BaseBean<QRDataBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), QRDataBean.class);
                //数据业务逻辑处理
                CustomNetCallBack<QRDataBean> callBack = new CustomNetCallBack(getContext(), baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<QRDataBean>() {

                    @Override
                    public void success(QRDataBean dataBean) {
                        createQRImage(dataBean.getQrcode(), simpleDraweeView);
                        description.setText(dataBean.getDescription());
                    }

                    @Override
                    public void fail(String errorCode) {

                    }

                }).dealData();
            }
        });
    }
}
